<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.util.Vector"%>
<%@ page import="jhu.profiteer.data.Account"%>
<%@ page import="jhu.profiteer.data.Airplane"%>
<%@ page import="jhu.profiteer.data.AirplaneJpaController"%>
<%@ page import="jhu.profiteer.data.Character"%>
<%@ page import="jhu.profiteer.data.CharacterJpaController"%>

<html>
  <%
    String pageTitle = "Character Information";
    String cssFile = "/profiteer-game/css/profiteer.css";
  %>
  <%@ include file="/WEB-INF/jspf/admin/header.jspf" %>
  <body>
    <%@ include file="/WEB-INF/jspf/title.jspf" %>
    <div class="contentArea Body">
      <div class="pagetitle">Character Information</div>
      <div class="pagebody">
        <table>
          <tr>
            <%
              //check for a session attribute named “character” of type Character.
              Character character = (Character)session.getAttribute("character");
              //check for a session attribute named “account” of type Account
              Account account = (Account)session.getAttribute("account");

              //If no Character object is found in the session
              if(null == character)
              {
                /* If no Account object is found in the session
                 *  redirect the user to the Index page
                 */
                if(null == account)
                {
            %>
            <jsp:forward page="/index" />
            <%
                } //if there is an account
                else
                {
                  // search the database for a Character with an accountId
                  // equal to the id field of the Account object
                  CharacterJpaController cjc = new CharacterJpaController();
                  if(cjc.testForCharacterWithAccountId(account.getId()))
                    character = cjc.findCharacterByAccountId(account.getId());
                  else
                  //If character is NOT found
                  {
            %>
            <td>
              <div class="divround">
                <%@ include file="/WEB-INF/jspf/admin/notice.jspf" %>
                <form action="character" method="post">
                    <h1>Create your Character</h1>
                    <table>
                        <tr>
                            <td>Character Name:</td>
                            <td><input type="text" name="characterName"
                                       value="Enter Character Name" /></td>
                        </tr>
                        <tr>
                            <td>Airplane Name:</td>
                            <td><input type="text" name="airplaneName"
                                       value="Enter Airplane Name" /></td>
                        </tr>
                        <tr>
                            <td>Description:</td>
                            <td><textarea name="description" cols="30" rows="5"></textarea></td>
                        </tr>

                    </table
                    <p>
                        <input type="submit" name="submit"
                               value="Create Character" />
                    </p>
                </form>
              </div>
            </td>
            <%
                  }
                }
              }

              //If a Character is found (either in the database or the session)
              if(null != character)
              {
            %>
            <td id="menu"><%@ include file="/WEB-INF/jspf/menu.jspf" %></td>
            <td>
              <div class="divround">
                <%@ include file="/WEB-INF/jspf/admin/notice.jspf" %>
            <%
                //check for a session attribute named “airplane” of type Airplane
                Airplane airplane = (Airplane)session.getAttribute("airplane");

                //If no Airplane object is found in the session
                if(null == airplane)
                {
                  // search the database for the Airplane with a characterId equal to the id field in the Character object
                  AirplaneJpaController ajc = new AirplaneJpaController();
                  if(ajc.testForAirplaneWithCharacterId(character.getId()))
                    airplane = ajc.findAirplaneByCharacterId(character.getId());
                  else
                  //If no Airplane is found in the database
                  {
                    account.setDamaged(true);
                    // db transaction should be created , the account object saved to the db, the transaction commited
                    if (null == notices) notices = new Vector<String>();
                    notices.add("This account has been damaged and needs to be reviewed by a game administrator. An administrator will contact you once the account has been examined and a solution has been implemented.");
                    session.setAttribute("notices", notices);
            %>
            <jsp:forward page="/logout" />
            <%
                  }
                }

                session.setAttribute("character", character);
                session.setAttribute("airplane", airplane);
                /* displays the Menu JSP fragment, a label that says
                 * “Character Name” followed by the name field of the
                 * Character object, a label that says “Airplane Name”
                 * followed by the name field of the Airplane object as
                 * a hyperlink to the Airplane page */
                //************ code omitted *********************
            %>
                <fieldset>
                  <legend>Character Information</legend>
                  <form action="character" method="post">
                    <table>
                      <tr>
                        <td>Character Name:</td>
                        <td><%= character.getName() %></td>
                      </tr>
                      <tr>
                        <td>Airplane Name:</td>
                        <td><a href="/profiteer-game/airplaneInformation"><%= airplane.getName() %></a></td>
                      </tr>
                      <tr>
                        <td><img src="/profiteer-game/images/gold.jpg" alt="gold" width="40" height="40"/></td>
                        <td><%= character.getMoney() %></td>
                      </tr>
                      <tr>
                        <td>Description:</td>
                        <%
                          if(character.getDescription() != null)
                          {
                        %>
                          <td><textarea name="description" cols="30" rows="5"><%= character.getDescription() %></textarea></td>
                        <%
                          }
                          else
                          {
                        %>
                          <td><textarea name="description" cols="30" rows="5"></textarea></td>
                        <%
                          }
                        %>
                      </tr>
                    </table>
                    <p><input type="submit" name="submit" value="Update Description" /></p>
                  </form>
                </fieldset>
            <%
              }
            %>
              </div>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <%@ include file="/WEB-INF/jspf/footer.jspf" %>
  </body>
</html>